﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace FaymanController.Utils
{
    public class Config
    {
        public bool otherTimeCanPlay { get; set; }
        public bool allowExtra { get; set; }
        public int extraMin { get; set; }
        public int extraSec { get; set; }
        /// <summary>
        /// 0 为右上角, 1 为全屏
        /// </summary>
        public int extraTimeType { get; set; }
        public string notifyContent { get; set; }
        /// <summary>
        /// 依照控件顺序依次为 0, 1, 2, 3
        /// </summary>
        public int stopType { get; set; }
    }
    public static class ConfigLoader
    {
        /// <summary>
        /// 从一个指定的配置文件载入程序配置
        /// </summary>
        /// <param name="ConfigFileName"></param>
        public static Config LoadConfig(string ConfigFileName)
        {
            Config config;
            try
            {
                config = JsonConvert.DeserializeObject<Config>(FileIO.ReadFile(ConfigFileName));
            }
            catch (Exception)
            {
                return null;
            }
            return config;
        }
        /// <summary>
        /// 从一个指定的配置文件载入时间表
        /// </summary>
        /// <param name="TimeTableFileName"></param>
        /// <returns></returns>
        public static List<TimeData> LoadTimeTable(string TimeTableFileName)
        {
            List<TimeData> timetable;
            try
            {
                timetable = JsonConvert.DeserializeObject<List<TimeData>>(FileIO.ReadFile(TimeTableFileName));
            }
            catch (Exception)
            {
                return null;
            }
            return timetable;
        }
        /// <summary>
        /// 将一个配置项对象保存到指定的配置文件中
        /// </summary>
        /// <param name="ConfigFileName"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static bool SaveConfig(string ConfigFileName, Config config)
        {
            string ConfigString;
            try
            {
                ConfigString = JsonConvert.SerializeObject(config);
            }
            catch (Exception)
            {
                return false;
            }
            try
            {
                FileIO.WriteFile(ConfigFileName, ConfigString);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 将一个时间表对象保存到指定的时间表配置文件中
        /// </summary>
        /// <param name="ConfigFileName"></param>
        /// <param name="timetable"></param>
        /// <returns></returns>
        public static bool SaveTimeTable(string ConfigFileName, List<TimeData> timetable)
        {
            string TimeTableString;
            try
            {
                TimeTableString = JsonConvert.SerializeObject(timetable);
            }
            catch (Exception)
            {
                return false;
            }
            try
            {
                FileIO.WriteFile(ConfigFileName, TimeTableString);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
